package com.xzit.service;

import com.xzit.entity.Account;
import com.xzit.entity.Record;
import com.xzit.mapper.AccountMapper;
import com.xzit.mapper.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("accountServiceXml")
public class AccountServiceImpl_Xml implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public void create(Account account) {
        account.setCoupon(0);
        accountMapper.insert2(account);
    }

    @Override
    public List<Account> list() {
        return accountMapper.selectAll();
    }

    @Override
    public Account login(String account, String password) {
        return accountMapper.login(account, password);
    }

    @Override
    public void recharge(String account, int amount) throws Exception {
        Record r = new Record();
        r.setAccount(account);
        r.setUpdateTime(new java.util.Date());
        r.setCategory("充值");
        r.setNum(amount);
        recordMapper.insert(r);
        accountMapper.updateCoupon(account, amount);
        Thread.sleep(6000);
        throw new RuntimeException("充值延迟过久，已回滚");


    }

    @Override
    public void consume(String account, int amount) {
        Record r = new Record();
        r.setAccount(account);
        r.setUpdateTime(new java.util.Date());
        r.setCategory("消费");
        r.setNum(amount);
        recordMapper.insert(r);
        accountMapper.updateCoupon(account, -amount);
    }
}
